<?php

namespace app\admin\behavior;

use think\Cache;
use think\Request;
use think\Session;
use think\Config;

class Rights
{

    public function run(&$params)
    {

        //权限验证
        $t_data = [
            "code"=>0,
            "msg"=>"无访问权限"
        ];;

        $modeule = strtolower($params->module());
        //当前控制器名称
        $controller = strtolower($params->controller());
        //当前操作名称
        $action = strtolower($params->action());
        $actions = $modeule."/".$controller."/".$action;
        $sid = trim($params->param("admin_sid"));
        $rows = Cache::get($sid); //获取用户权限
        $role_id = $rows['admin_group_id'];//权限Id
        if(empty($role_id)){
            exit(json_encode(resJson(0,"无访问权限")->getData()));
        }

        /**
         *  过滤配置组的权限访问
         */
        $auth = new \app\admin\model\Privilege();
        $RoleModel = new \app\admin\model\Role();
        $parentNode = $auth->where("is_config",'1')->select();
        $parentNode = json_decode(json_encode($parentNode),true);

        $RoleResult = $RoleModel->where("id", $role_id)->find();

        $authResult = $auth->where('id','in',$RoleResult['privilege'])->select();

        $authResult = json_decode(json_encode($authResult),true);

        w_s("authResult",array_column($authResult,"id"));

        foreach($parentNode as $kNode => $vNode){
                    if($actions == $vNode['action']){
                            return true;
                    }
        }



        /**
         *  超级管理员的过滤
         */


        if($RoleResult['privilege']==="all"){

            return true;
        }
        if(empty($RoleResult['privilege'])){
            exit(json_encode(resJson(0,"无访问权限")->getData()));
        }

        /**
         *  用户拥有的权限
         */



        foreach ($authResult as $k => $v){
            if($actions == $v['action']){

                return true;
            }
        }


        exit(json_encode(resJson(0,"无访问权限")->getData()));










    }

}
